/**
 * Copyright (c) 2013 Axa Holding Belgium, SA. All rights reserved.
 * This software is the confidential and proprietary information of the AXA Group.
 */
package chabernac.gravidroid.model;

public class Rectangle {
	public float topLeftX;
	public float topLeftY;
	public float bottomRightX;
	public float bottomRightY;

	public Rectangle() {
	}

	private Rectangle(Vector aTopLeft, Vector aBottomRight) {
		topLeftX = aTopLeft.x;
		topLeftY = aTopLeft.y;
		bottomRightX = aBottomRight.x;
		bottomRightY = aBottomRight.y;
	}

	public static Rectangle constructFromTopLeftAndBottomRight(Vector aTopLeft, Vector aBottomRight) {
		return new Rectangle(aTopLeft, aBottomRight);
	}

	public static Rectangle constructFrom2Points(Vector ap1, Vector ap2) {
	    Rectangle theRectangle = new Rectangle();
	    theRectangle.topLeftX = ap1.x < ap2.x ? ap1.x : ap2.x;
	    theRectangle.topLeftY = ap1.y < ap2.y ? ap1.y : ap2.y;
	    theRectangle.bottomRightX = ap1.x > ap2.x ? ap1.x : ap2.x;
	    theRectangle.bottomRightY = ap1.y > ap2.y ? ap1.y : ap2.y;
		return theRectangle;
	}


	public boolean contains(Vector aPoint) {
		if (aPoint.x < topLeftX)
			return false;
		if (aPoint.y < topLeftY)
			return false;
		if (aPoint.x > bottomRightX)
			return false;
		if (aPoint.y > bottomRightY)
			return false;
		return true;
	}

	// I/System.out(20374): topleft: 337.02652,440.70343 bottomright:
	// 3454.2856,3887.12
	// 87 intersects with topleft: 680.4983,581.45404 bottomright:
	// 4039.8164,3915.1558

	public boolean intersectsWith(Rectangle anotherRect) {
		try {
			// System.out.println( toString() + " intersects with " +
			// anotherRect.toString() );

			// System.out.println( "bottomRight.x < anotherRect.topLeft.x: " + (
			// bottomRight.x < anotherRect.topLeft.x ) );
			if (bottomRightX < anotherRect.topLeftX) {
				// System.out.println( "Return false 1" );
				return false;
			}

			// System.out.println( "Rect.bottomRight.x < topLeft.x: " + (
			// anotherRect.bottomRight.x < topLeft.x ) );
			if (anotherRect.bottomRightX < topLeftX) {
				// System.out.println( "Return false 2" );
				return false;
			}

			// System.out.println( "topLeft.y < anotherRect.bottomRight.y :" + (
			// topLeft.y < anotherRect.bottomRight.y ) );
			if (topLeftY > anotherRect.bottomRightY) {
				// System.out.println( "Return false 3" );
				return false;
			}

			// System.out.println( "anotherRect.topLeft.y < bottomRight.y: " + (
			// anotherRect.topLeft.y < bottomRight.y ) );
			if (anotherRect.topLeftY > bottomRightY) {
				// System.out.println( "Return false 4" );
				return false;
			}
		} catch (Throwable e) {
			e.printStackTrace();
		}
		// System.out.println( "Return true" );
		return true;
	}

	public Rectangle addBorder(float aBorder) {
		Vector theTopLeft = new Vector(topLeftX - aBorder, topLeftY + aBorder);
		Vector theBottomRight = new Vector(bottomRightX + aBorder, bottomRightY - aBorder);
		return Rectangle.constructFromTopLeftAndBottomRight(theTopLeft, theBottomRight);
	}

	@Override
	public String toString() {
		StringBuilder theBuilder = new StringBuilder();
		theBuilder.append("topleft: ");
		theBuilder.append(topLeftX);
		theBuilder.append(",");
		theBuilder.append(topLeftY);
		theBuilder.append(" bottomright: ");
		theBuilder.append(bottomRightX);
		theBuilder.append(",");
		theBuilder.append(bottomRightY);
		return theBuilder.toString();
	}
}
